package com.newway.back.controller.approval;

import com.newway.back.dto.approval.TemplateAddDto;
import com.newway.back.entity.approval.ApprovalNode;
import com.newway.back.entity.approval.ApprovalTemplate;
import com.newway.back.enums.ApproverTypeEnum;
import com.newway.back.mapper.approval.ApprovalNodeMapper;
import com.newway.back.mapper.approval.ApprovalTemplateMapper;
import com.newway.back.util.SecurityUserContext;
import com.newway.common.Result;
import com.newway.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/back/approval/template")
@RestController
public class ApprovalTemplateController {

    @Resource
    ApprovalTemplateMapper approvalTemplateMapper;

    @Resource
    ApprovalNodeMapper approvalNodeMapper;

    /**
     * 查询流程模板
     * @return
     */
    @GetMapping("/list")
    public Result getList(){
        List<ApprovalTemplate> approvalTemplates = approvalTemplateMapper.selectList(null);
        return Result.success(approvalTemplates);
    }

    @PostMapping("/add")
    @Transactional
    public Result addTemplate(@RequestBody TemplateAddDto templateAddDto){
        int defaultVersion = 1;
        ApprovalTemplate approvalTemplate = new ApprovalTemplate();
        approvalTemplate.setTemplateName(templateAddDto.getTemplateName());
        approvalTemplate.setDescription(templateAddDto.getRemarks());
        approvalTemplate.setCreatedAt(LocalDateTime.now());
        approvalTemplate.setCreatedBy(SecurityUserContext.getCurrentUserInfo().getId());
        approvalTemplate.setIsActive("0");//0未生效  1 生效
        approvalTemplate.setVersion(defaultVersion);
        approvalTemplateMapper.insert(approvalTemplate);
        List<ApprovalNode> approvalNodeList = templateAddDto.getApprovalNodeList();
        approvalNodeList.forEach(approvalNode -> {
            //approvalNode.setApproverValue();
            //approvalNode.setNodeLevel();
            approvalNode.setApproverType(ApproverTypeEnum.POSITION.getValue());
            approvalNode.setTemplateId(approvalTemplate.getTemplateId());
            approvalNode.setVersion(defaultVersion);
            approvalNode.setNodeName("指定角色审批");//待定 修改
            approvalNode.setCreatedAt(LocalDateTime.now());
            approvalNodeMapper.insert(approvalNode);
        });
        return Result.success();
    }

    /**
     * 模板停用启动
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/publish/{id}/{status}")
    public Result templatePublish(@PathVariable("id") Integer id ,@PathVariable("status") String status){
        ApprovalTemplate approvalTemplate = approvalTemplateMapper.selectById(id);
        if(approvalTemplate==null){
            throw new BusinessException("当前数据不存在");
        }
        approvalTemplate.setIsActive(status);
        approvalTemplateMapper.updateById(approvalTemplate);
        return Result.success();
    }
}
